<%@page language="java" import="java.sql.*" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<fmt:setBundle basename="messages.main"/>
<%
Integer userId=(Integer)request.getSession(false).getAttribute("id");
dbBean.DBConnectionManager connMgr=(dbBean.DBConnectionManager)application.getAttribute("connMgr");
if((userId==null) || (connMgr==null)) {
  application.log("- No existe una sesion de usuario o no se ha podido obtener una conexion con la BBDD");
  response.sendRedirect("/VEGPS/jsp/index.jsp");
} else {
StringBuffer fechas=new StringBuffer();

application.log("- Consultamos las fechas con datos en la BBDD");
Connection con = connMgr.getConnection("mysql");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT DISTINCT DATE(timestamp) FROM datos WHERE userId="+userId+";");
java.sql.Date d=null;
while(rs.next()) {
	fechas.append("#");
	d=rs.getDate(1);
	fechas.append(d.getYear()+1900);
	fechas.append("-");
	fechas.append(d.getMonth());
	fechas.append("-");
	fechas.append(d.getDate());
}
fechas.append("#");
rs.close();
stmt.close();
connMgr.freeConnection("mysql",con);
application.log("- Liberada la conexion con la BBDD");

if(fechas.length()<=1) {
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>GPSou</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
  a {color: black}
  p.bold {font-weight: bold}
  div {text-align: center}
</style>
</head>
<body>
  <p style="text-align: center"><img src="/VEGPS/html/images/GPSou-logo-banner.png" alt="GPS-o&ugrave;"></p>
<div style="text-align: center">


<table border="0" width="100%" cellspacing="0" cellpadding="0" style="margin-left: auto; margin-right: auto">
	<tr>
	
<td><br><p class="bold"><fmt:message key="nodata"/></p></td></tr>
</table>
<p><a href="/VEGPS/jsp/CambiarDatos.jsp"><fmt:message key="menu.1"/></a> | <a href="/VEGPS/ServletGoogleGPS?p=desconectar"><fmt:message key="menu.2"/></a></p>
  <p>
    <img src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Strict" height="31" width="88">
  </p>
</div>
	
</body>
</html>
<%
} else {
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>




<style type="text/css">@import url(/VEGPS/html/jscalendar-1.0/calendar-system.css);</style>
<script type="text/javascript" src="/VEGPS/html/jscalendar-1.0/calendar.js"></script>
<script type="text/javascript" src="/VEGPS/html/jscalendar-1.0/lang/calendar-en.js"></script>
<script type="text/javascript" src="/VEGPS/html/jscalendar-1.0/calendar-setup.js"></script>

<script type="text/javascript">
<!--
var fechas='<%=fechas%>';
var ultimaFechaDesde=new Date(<%=d.getYear()+1900%>, <%=d.getMonth()%>, <%=d.getDate()%>, 0, 0);
var ultimaFechaHasta=new Date(<%=d.getYear()+1900%>, <%=d.getMonth()%>, <%=d.getDate()%>, 23, 59, 59);
var desdeDate, hastaDate;

function borrar() {
	document.mapaFrm.desde.value="";
	document.mapaFrm.hasta.value="";
	return(false);
}

function dateStatusHandler(date, y, m, d) {
	var dia=y + "-" + m + "-" + d + "#";
	if(fechas.indexOf(dia)!=-1)
		return(false);
	else
		return(true);
}

function setDate(cal) {
	if(cal.dateClicked) {
	  var p=cal.params;
	  p.inputField.value=cal.date.print(p.ifFormat);
	  p.displayArea.value=cal.date.toLocaleString();
	  cal.hide();
	}
}

function setStartDate(cal) {
	desdeDate=cal.date;
	setDate(cal);
}

function setFinishDate(cal) {
	hastaDate=cal.date;
	setDate(cal);
}

function fill(n) {
	if(n<10)
	  return("0"+n);
	else
	  return(n);
}

function adjustUTCDates() {
	if(desdeDate != undefined) {
		document.mapaFrm.desde.value=desdeDate.getUTCFullYear()+
				"-"+fill((desdeDate.getUTCMonth()+1))+
				"-"+fill(desdeDate.getUTCDate())+
				" "+fill(desdeDate.getUTCHours())+
				":"+fill(desdeDate.getUTCMinutes())+
				":00.0";
	}

	if(hastaDate != undefined) {
		document.mapaFrm.hasta.value=hastaDate.getUTCFullYear()+
				"-"+fill((hastaDate.getUTCMonth()+1))+
				"-"+fill(hastaDate.getUTCDate())+
				" "+fill(hastaDate.getUTCHours())+
				":"+fill(hastaDate.getUTCMinutes())+
				":00.0";
	}
}

function repetirRuta() {
	adjustUTCDates();
	var desdeStr=document.getElementById("ifDesde").value;
	var hastaStr=document.getElementById("ifHasta").value;
	if(desdeStr=='') {
			alert("<fmt:message key="msg.1"/>");
			return;
	}
	if(hastaStr!='') {
		if(hastaStr < desdeStr) {
			alert("<fmt:message key="msg.2"/>");
			return;
		}
        }
	document.mapaFrm.action='/VEGPS/jsp/GoogleGPS_offline.jsp';
	document.mapaFrm.submit();
}

function mostrarRecorrido() {
	adjustUTCDates();
	var desdeStr=document.getElementById("ifDesde").value;
	var hastaStr=document.getElementById("ifHasta").value;
	if(desdeStr=='') {
			alert("<fmt:message key="msg.1"/>");
			return;
	}
	if(hastaStr!='') {
		if(hastaStr < desdeStr) {
			alert("<fmt:message key="msg.2"/>");
			return;
		}
        }
	document.mapaFrm.action='/VEGPS/jsp/GoogleGPS_recorrido.jsp';
	document.mapaFrm.submit();
}

-->
</script>

<style type="text/css">
  .lines {border-width: 1px;
	border-style: outset;
	border-color: black;
	border-collapse: collapse}
  td {text-align: left}
  p.bold {font-weight: bold}
  a:link IMG, a:visited IMG { 
        border-style: none}
  a {color: black}
</style>


<meta http-equiv="Content-Language" content="es">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title><fmt:message key="title"/></title></head><body>
  <p style="text-align: center"><img src="/VEGPS/html/images/GPSou-logo-banner.png" alt="GPS-o&ugrave;"></p>

<div style="text-align: center">
<form name="mapaFrm" action="" method="post">
  <input name="desde" id="desde" type="hidden">
  <input name="hasta" id="hasta" type="hidden">
</form>
<input name="ifDesde" id="ifDesde" type="hidden">
<input name="ifHasta" id="ifHasta" type="hidden">

<table cellpadding="5" cellspacing="0" border="0" style="margin-left: auto; margin-right: auto">
  <tr>
    <td rowspan="2" valign="top" style="background: #FFFF99">
	<p class="bold"><fmt:message key="real"/></p><p>
    <a href="/VEGPS/jsp/GoogleGPS.jsp" title="<fmt:message key="real.link.title"/>"><fmt:message key="real.link"/></a></p></td>
    <td rowspan="2" valign="top" style="background: #FFFF99">
    <img src="/VEGPS/html/images/GPSou-seguimiento.gif" width="200" height="165" alt="<fmt:message key="real.img.title"/>"></td>
    <td valign="top" style="background: #99FF99"><p class="bold"><fmt:message key="saved"/></p><p>
    <fmt:message key="saved.text.1"/></p>
    <fmt:message key="saved.text.2"/><br>
&nbsp;<table style="border-collapse: collapse;" id="AutoNumber2" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td><fmt:message key="saved.start"/></td>
        <td align="right"><input name="daDesde" id="daDesde" title="<fmt:message key="saved.start.textbox"/>" readonly="readonly" size="45" type="text"></td>
        <td><input value="(...)" id="desdeBtn" title="<fmt:message key="saved.start.button"/>" type="button">
<script type="text/javascript">
        Calendar.setup({
                date : ultimaFechaDesde,
                inputField : "ifDesde",
		displayArea: "daDesde",
                ifFormat : "%Y-%m-%d %H:%M:%S.0",
                daFormat : "%A, %B %d, %Y",
                showsTime: true,
                button : "desdeBtn",
                firstDay : 1,
                electric : false,
                dateStatusFunc : dateStatusHandler,
		onSelect: setStartDate
        });
</script>
	</td>
      </tr>
      <tr>
        <td><fmt:message key="saved.end"/></td>
        <td align="right"><input name="daHasta" id="daHasta" title="<fmt:message key="saved.end.textbox"/>" readonly="readonly" size="45" type="text"></td>
        <td><input value="(...)" id="hastaBtn" title="<fmt:message key="saved.end.button"/>" type="button">
<script type="text/javascript">
        Calendar.setup({
                date : ultimaFechaHasta,
                inputField : "ifHasta",
		displayArea: "daHasta",
                ifFormat : "%Y-%m-%d %H:%M:%S.0",
		daFormat : "%A, %B %d, %Y",
                showsTime: true,
                button : "hastaBtn",
                firstDay : 1,
                electric : false,
                dateStatusFunc : dateStatusHandler,
		onSelect: setFinishDate
        });
</script>
	</td>
      </tr>
    </table>
    </td>
    <td valign="top" style="background: #99FF99">
	<img src="/VEGPS/html/images/GPSou-guardado.gif" width="207" height="189" alt="<fmt:message key="saved.img.title"/>"></td>
  </tr>
  <tr>
    <td colspan="2" valign="top" style="background: #99FF99">
    <hr>
    <p><fmt:message key="saved.text.3"/></p>
    <ul>
      <li><a href="javascript: repetirRuta()" title="<fmt:message key="saved.link.1.title"/>"><fmt:message key="saved.link.1"/></a></li>
      <li><a href="javascript: mostrarRecorrido()" title="<fmt:message key="saved.link.2.title"/>"><fmt:message key="saved.link.2"/></a></li>
    </ul>
    </td>
  </tr>
</table>
<p><a href="/VEGPS/jsp/CambiarDatos.jsp" title="<fmt:message key="menu.1.title"/>"><fmt:message key="menu.1"/></a> | <a href="/VEGPS/ServletGoogleGPS?p=desconectar" title="<fmt:message key="menu.2.title"/>"><fmt:message key="menu.2"/></a></p>
  <p>
    <img src="/VEGPS/html/images/valid-html401.png" alt="Valid HTML 4.01 Strict" height="31" width="88">
  </p>
</div>
</body></html>
<%
}
}
%>
